﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
using System.Net;

namespace CrazyTalk.Common
{
    /// <summary>
    /// 发送邮件类 的摘要说明
    /// </summary>
    /// <remarks></remarks>
    public class MailHelper
    {
        #region 数据成员
        //收件人地址
        /// <summary>
        /// 
        /// </summary>
        private string m_To = "";
        //发件人地址
        /// <summary>
        /// 
        /// </summary>
        private string m_From = "";
        //邮件标题
        /// <summary>
        /// 
        /// </summary>
        private string m_Subject = "";
        //邮件正文
        /// <summary>
        /// 
        /// </summary>
        private string m_Body = "";
        //发送服务器名或地址
        /// <summary>
        /// 
        /// </summary>
        private string m_Host = "";
        //发件人用户名
        /// <summary>
        /// 
        /// </summary>
        private string m_UserName = "";
        //发件人密码
        /// <summary>
        /// 
        /// </summary>
        private string m_Password = "";
        //邮件附件
        /// <summary>
        /// 
        /// </summary>
        private string m_File = "";
        //內容是否未HTML格式
        /// <summary>
        /// 
        /// </summary>
        private bool m_IsBodyHtml = true;
        #endregion

        #region 构造函数
        /// <summary>
        /// Initializes a new instance of the <see cref="MailHelper"/> class. 然後给屬性赋值
        /// </summary>
        /// <remarks></remarks>
        public MailHelper() { }

        /// <summary>
        /// 构造函数重载
        /// </summary>
        /// <param name="to">To.</param>
        /// <param name="from">From.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="host">The host.</param>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <remarks></remarks>
        public MailHelper(string to, string from, string subject, string body, string host, string userName, string password, string fileName)
        {
            m_To = to;
            m_From = from;
            m_Subject = subject;
            m_Body = body;
            m_Host = host;
            m_UserName = userName;
            m_Password = password;
            m_File = fileName;
        }
        #endregion

        #region 数据属性
        /// <summary>
        /// 收件人邮箱地址
        /// </summary>
        /// <value>The TO.</value>
        /// <remarks></remarks>
        public string TO
        {
            get
            {
                return m_To;
            }
            set
            {
                m_To = value;
            }
        }

        /// <summary>
        /// 发件人邮箱地址
        /// </summary>
        /// <value>From.</value>
        /// <remarks></remarks>
        public string From
        {
            get
            {
                return m_From;
            }
            set
            {
                m_From = value;
            }
        }
        
        /// <summary>
        /// 邮件标题
        /// </summary>
        /// <value>The subject.</value>
        /// <remarks></remarks>
        public string Subject
        {
            get
            {
                return m_Subject;
            }
            set
            {
                m_Subject = value;
            }
        }     
        /// <summary>
        /// 邮件正文
        /// </summary>
        /// <value>The body.</value>
        /// <remarks></remarks>
        public string Body
        {
            get
            {
                return m_Body;
            }
            set
            {
                m_Body = value;
            }
        }

        /// <summary>
        /// 主机名
        /// </summary>
        /// <value>The host.</value>
        /// <remarks></remarks>
        public string Host
        {
            get
            {
                return m_Host;
            }
            set
            {
                m_Host = value;
            }
        }

        /// <summary>
        /// 发件人的邮箱用户名
        /// </summary>
        /// <value>The name of the user.</value>
        /// <remarks></remarks>
        public string UserName
        {
            get
            {
                return m_UserName;
            }
            set
            {
                m_UserName = value;
            }
        }
        
        /// <summary>
        /// 发件人的邮箱密码
        /// </summary>
        /// <value>The password.</value>
        /// <remarks></remarks>
        public string Password
        {
            get
            {
                return m_Password;
            }
            set
            {
                m_Password = value;
            }
        }

        
        /// <summary>
        ///邮件附件
        /// </summary>
        /// <value>The file.</value>
        /// <remarks></remarks>
        public string File
        {
            get
            {
                return m_File;
            }
            set
            {
                m_File = value;
            }
        }

        /// <summary>
        /// 內容是否未HTML格式 默認為true
        /// </summary>
        /// <value><c>true</c> if this instance is body HTML; otherwise, <c>false</c>.</value>
        /// <remarks></remarks>
        public bool IsBodyHtml
        {
            get
            {
                return m_IsBodyHtml;
            }
            set
            {
                m_IsBodyHtml = value;
            }
        }

        #endregion

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <returns>发送是否成功</returns>
        /// <remarks></remarks>
        public bool Send()
        {
            try
            {
                //获取所有的收件人地址
                char[] ch = { ',' };
                string[] address = m_To.Split(ch);
                MailAddressCollection allAddress = new MailAddressCollection();
                for (int i = 0; i < address.Length; i++)
                {
                    //收件人地址
                    MailAddress toAddress = new MailAddress(address[i]);
                    allAddress.Add(toAddress);

                    //发件人地址
                    MailAddress fromAddress = new MailAddress(m_From);

                    //定义邮件消息
                    MailMessage msg = new MailMessage(fromAddress, toAddress);
                    //邮件标题
                    msg.Subject = m_Subject;
                    //邮件正文
                    msg.Body = m_Body;

                    //內容是否未HTML格式
                    msg.IsBodyHtml = m_IsBodyHtml;

                    //获取所有邮件附件
                    char[] cr = { ';' };
                    string[] file = m_File.Split(cr);
                    for (int n = 0; n < file.Length; n++)
                    {
                        if (file[n] != "")
                        {
                            //附件对象
                            Attachment data = new Attachment(file[n], MediaTypeNames.Application.Octet);
                            //附件资料
                            ContentDisposition disposition = data.ContentDisposition;
                            disposition.CreationDate = System.IO.File.GetCreationTime(file[n]);
                            disposition.ModificationDate = System.IO.File.GetLastWriteTime(file[n]);
                            disposition.ReadDate = System.IO.File.GetLastAccessTime(file[n]);
                            //加入邮件附件
                            msg.Attachments.Add(data);
                        }
                    }

                    //使用简单邮件传输协议来传送邮件
                    SmtpClient sendMail = new SmtpClient();
                    //发送邮件的服务器名或地址
                    sendMail.Host = m_Host;
                    //验证发件人的身份
                    sendMail.Credentials = new NetworkCredential(m_UserName, m_Password);
                    //处理待发送邮件的方法
                    sendMail.DeliveryMethod = SmtpDeliveryMethod.Network;

                    //发送邮件
                    sendMail.Send(msg);
                }
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("EMail发送失败" + ex);
            }
        }
    }
}
